import ioc.bean.ProfileData;
import ioc.config.AutowiredConifg;
import ioc.config.JSRConfig;
import ioc.config.ProfileDataConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class T03_BeanAutomaticAssembly {
    AnnotationConfigApplicationContext context;

    /**
     * 1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
     * 2、代码的方式激活某种环境；
     */
    @Test
    public void profileTest() {
        // 1、创建一个applicationContext
        context = new AnnotationConfigApplicationContext();
        // 2、设置需要激活的环境
        context.getEnvironment().setActiveProfiles("dev");
        // 3、注册主配置类
        context.register(ProfileDataConfig.class);
        // 4、启动刷新容器
        context.refresh();

        String[] namesForType = context.getBeanNamesForType(ProfileData.class);
        for (String string : namesForType) {
            System.out.println(string);
        }
    }

    @Test
    public void autowired() {
        context = new AnnotationConfigApplicationContext(AutowiredConifg.class);
    }

    @Test
    public void jsrTest() {
        context = new AnnotationConfigApplicationContext(JSRConfig.class);
    }

    @AfterEach
    public void printBeans() {
        System.out.println("============容器中的对象==========");
        String[] definitionNames = context.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
        System.out.println("=================================");
    }
}
